#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import pandas as pd

from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.XRetryableSave import XRetryableSave
from sicost.AbstractDPJob import AbstractDPJob


class BZBZCBJob(AbstractDPJob):
    """
    通过两个SQL去取来源表和目标表的表结构

    传入6个参数   来源标  和目标表  及开始时间结束时间

    将来源表那段时间的数据取出来  在存到目标表里
    """

    def __init__(self,
                 p_config=None,
                 p_db_conn_rds=None,
                 p_db_conn_dbprod7=None):


        super(BZBZCBJob, self).__init__(p_config=p_config,
                                        p_db_conn_mpp=None,
                                        p_db_conn_rds=p_db_conn_rds,
                                        p_db_conn_dbprod7=p_db_conn_dbprod7,
                                        p_unit=None,
                                        p_account=None,
                                        p_cost_center=None,
                                        p_account_period_start=None,
                                        p_account_period_end=None)

        pass

    def do_execute(self):
        """
        """
        self.logger.info('BZBZCBJob.do_execute')

        sql = " DELETE FROM BGTARAS1.T_ADS_FACT_SICB_BZBZCB"

        self.logger.info(sql)
        self.db_conn_rds.execute(sql)





        sql = " SELECT " \
              " ACCOUNT ," \
              " COST_CENTER ," \
              " PRODUCT_CODE," \
              " DET," \
              " WCE," \
              " AMOUT_ACT" \
              " FROM " \
              " BGRAGGCB.TACACP3" \
              " WHERE 1=1 " \
              " AND ACCOUNT = '1001'" \
              " AND WCE = '65000'"
        self.logger.info(sql)

        df0 = XRetryableQuery(p_db_conn=self.db_conn_dbprod7, p_sql=sql, p_max_times=5).redo()
        success = df0.empty is False
        if success is False:
            return
        df0.columns = df0.columns.str.upper()


        df0.rename(columns={'ACCOUNT': 'ACCT'}, inplace=True)
        df0.rename(columns={'DET': 'PACK_MODE_CODE'}, inplace=True)
        df0.rename(columns={'WCE': 'COST_SUBJECT'}, inplace=True)
        df0.rename(columns={'AMOUT_ACT': 'UNIT_COST'}, inplace=True)





        XRetryableSave(p_db_conn=self.db_conn_rds, p_table_name='T_ADS_FACT_SICB_BZBZCB', p_schema='BGTARAS1',
                       p_dataframe=df0,
                       p_max_times=5).redo()



        super(BZBZCBJob, self).do_execute()
